package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sendSms.SendSms;
import com.ruoyi.common.utils.sendSms.Sms;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api("生成验证码接口")
@Controller
public class GenerateSms {
    @Autowired
    private RedisCache redisCache;

    private Logger logger = LoggerFactory.getLogger(getClass());


    @ApiOperation("生成验证码")
    @ApiImplicitParam(name = "mobile",value = "手机号码",required = true,dataType = "String",paramType = "query")
    @PostMapping("/sms/code")
    @ResponseBody
    public AjaxResult sms(@RequestBody LoginBody loginBody) throws Exception {

        String mobile=loginBody.getMobile();
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.SMS_CAPTCHA_CODE_KEY + uuid;

        int code = (int) Math.ceil(Math.random() * 9000 + 1000);
        Map<String, Object> map = new HashMap<>(16);
        map.put("mobile", mobile);
        map.put("code", code);
        redisCache.setCacheObject(verifyKey, map, Constants.SMS_EXPIRATION, TimeUnit.MINUTES);
        //发送短信验证码
        Sms sms = new Sms();
        sms.setPhoneNumbers(mobile);
        sms.setCode(Integer.toString(code));
        sms.setSingnName("徐州安评应急培训有限公司");
        sms.setTemplateCode("SMS_214620014");
        SendSms sendSms = new SendSms();
        String res = sendSms.sendSms(sms);
        logger.info(" 为 {} 设置短信验证码：{}", mobile, code);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("res", res);
        ajax.put("uuid", uuid);
        return ajax;
    }

    /**
     * 发送短信验证码
     * @param sms
     * @return
     * @throws Exception
     */
    @ApiOperation("生成验证码")
    @PostMapping("/sms/sendSms")
    @ResponseBody
    public AjaxResult sms(@RequestBody Sms sms) throws Exception {
        //发送短信验证码
        int code = (int) Math.ceil(Math.random() * 9000 + 1000);
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.SMS_CAPTCHA_CODE_KEY + uuid;
        redisCache.setCacheObject(verifyKey, code, Constants.SMS_EXPIRATION, TimeUnit.MINUTES);
        sms.setCode(Integer.toString(code));
        sms.setSingnName("徐州安评应急培训有限公司");
        SendSms sendSms = new SendSms();
        String res = sendSms.sendSms(sms);
        logger.info(" 为 {} 设置短信验证码：{}", sms.getPhoneNumbers(), code);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("uuid", uuid);
//        ajax.put("code", code);
        ajax.put("res", res);
        return ajax;
    }


    /**
     * 校验短信验证码
     */
    @ApiOperation("校验验证码")
    @GetMapping("/sms/checkCode")
    @ResponseBody
    public AjaxResult checkCode(@RequestParam("uuid") String uuid, @RequestParam("code") Integer code){
        String verifyKey = Constants.SMS_CAPTCHA_CODE_KEY + uuid;
        Integer checkcode = redisCache.getCacheObject(verifyKey);
        if(checkcode.equals(code)){
            redisCache.deleteObject(verifyKey);
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
}
